// This file is part of the Pascal Compiler source code
//
// Copyright (C) 2009 UFPB (http://www.di.ufpb.br),
// Federal University of Paraiba.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
// *****************************************************************

/*
 * File: ExtensionFileFilter.java
 */

package pascalcompiler.util;

import java.io.File;
import java.util.List;

/** A simple windows like file filter. */
public class ExtensionFileFilter extends javax.swing.filechooser.FileFilter {
    String[] extensions;
    String description;

    /**
     * Creates a file filter with several extensions.
     *
     * @param descr The filter description.
     * @param exts The extensions that the filter supports.
     */
    public ExtensionFileFilter(String descr, String... exts)
    {
        // clone and lowercase the extensions
        extensions = new String[exts.length];
        for (int i = exts.length - 1; i >= 0; i--)
            extensions[i] = exts[i].toLowerCase();

        // make sure we have a valid (if simplistic) description
        description = (descr == null ? exts[0] + " files" : descr);
    }

    /**
     * Creates a file filter with several extensions.
     *
     * @param descr The filter description.
     * @param exts The extensions that the filter supports.
     */
    public ExtensionFileFilter(String descr, List<String> exts)
    {
        this(descr, exts.toArray(new String[exts.size()]));
    }

    /**
     * Verify if its a valid file. This method is automatically called by the
     * <code>FileChooser</code> object.
     *
     * @param f The file to be verified.
     * @return A boolean indicated if the file was accepted by the filter or
     *         not.
     */
    @Override
    public boolean accept(File f)
    {
        // we always allow directories, regardless of their extension
        if (f.isDirectory())
            return true;

        // ok, it's a regular file so check the extension
        for (String extension : extensions)
            if (f.getName().toLowerCase().endsWith(extension))
                return true;

        return false;
    }

    /**
     * The description of this kind of files.
     *
     * @return file kind description
     */
    @Override
    public String getDescription()
    {
        return description;
    }
}